package com.ocom.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.human.EntityBizApply;
import com.ocom.common.entity.human.EntityBizLeaveApply;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.ugcapi.GzhUserLeaveList;
import com.ocom.common.utils.DateUtils;
import com.ocom.company.dao.IDaoBizLeaveApply;
import com.ocom.company.service.BizApplyService;
import com.ocom.company.service.BizLeaveApplyService;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class BizLeaveApplyServiceImpl extends ServiceImpl<IDaoBizLeaveApply, EntityBizLeaveApply> implements BizLeaveApplyService {


    @Autowired
    BizApplyService bizApplyService;

    @Autowired
    IDaoBizLeaveApply iDaoBizLeaveApply;


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public EntityBizLeaveApply addApply(Long companyId, EntityBizLeaveApply entityBizLeaveApply) {
        boolean sa =  this.save(entityBizLeaveApply);
        if(sa == true){
            EntityBizApply entityBizApply =new EntityBizApply();
            entityBizApply.setComId(companyId);
            entityBizApply.setPId(entityBizLeaveApply.getPId());
            entityBizApply.setDefKey(entityBizLeaveApply.getDefKey());
            entityBizApply.setApplyTime(new Date());
            entityBizApply.setType(entityBizLeaveApply.getType());
            entityBizApply.setBusinessId(entityBizLeaveApply.getId());
            entityBizApply.setTitle(entityBizLeaveApply.getTitle());
            bizApplyService.addApply(companyId , entityBizApply);
            return  entityBizLeaveApply;
        }
        return null;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public boolean updateApply(Long companyId, EntityBizLeaveApply entityBizLeaveApply) {
        if(null!=entityBizLeaveApply.getStatus() && entityBizLeaveApply.getStatus()==3){
            EntityBizLeaveApply entityBizLeaveApply1 = this.getById(entityBizLeaveApply.getId());
            if(entityBizLeaveApply1.getStatus()==3){
                return true;
            }
            if(entityBizLeaveApply1.getStatus()!=0){
                return false;
            }
        }
//        System.out.println("{entityBizLeaveApply}::"+entityBizLeaveApply);
        entityBizLeaveApply.setUpdateTime(new Date());
        boolean sa =  this.updateById(entityBizLeaveApply);
//        if(sa == true){
            EntityBizApply entityBizApply =new EntityBizApply();
            entityBizApply.setComId(companyId);
            entityBizApply.setBusinessId(entityBizLeaveApply.getId());
            entityBizApply.setStatus(entityBizLeaveApply.getStatus());
            entityBizApply.setActivitiKey(entityBizLeaveApply.getActivitiKey());
            bizApplyService.updateApply(companyId , entityBizApply);
//            return  true;
//        }
        return sa;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public PageInfo getApplyList(Long companyId, GzhUserLeaveList gzhUserLeaveList) {
        PageHelper.startPage(gzhUserLeaveList.getPage(), gzhUserLeaveList.getSize());
        List<EntityBizLeaveApply> list=iDaoBizLeaveApply.getList(gzhUserLeaveList.getDefKey(),
                gzhUserLeaveList.getComId(),
                gzhUserLeaveList.getPId(),
                gzhUserLeaveList.getIds());
        PageInfo<EntityBizLeaveApply> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public EntityBizLeaveApply getApplyOne(Long companyId, EntityBizLeaveApply entityBizLeaveApply) {
        return  iDaoBizLeaveApply.getInfo(entityBizLeaveApply.getId().intValue());
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Integer queryUserLeave(Long companyId, Long pId) {
        QueryWrapper<EntityBizLeaveApply> queryWrapper = new QueryWrapper<>();
        Date nowDate = DateUtils.getNowDate();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("p_id", pId);
        queryWrapper.eq("status", 1);
        queryWrapper.le("start_time", nowDate)
                .ge("end_time", nowDate);
        return this.count(queryWrapper);
//        return iDaoBizLeaveApply.getUserLeave(companyId,pId);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean save(Long companyId, EntityBizLeaveApply entityBizLeaveApply) {
        return this.save(entityBizLeaveApply);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean update(Long companyId, EntityBizLeaveApply entityBizLeaveApply) {
        return this.update(entityBizLeaveApply, Wrappers.lambdaQuery(EntityBizLeaveApply.class)
                .eq(EntityBizLeaveApply::getComId,entityBizLeaveApply.getComId())
                .eq(EntityBizLeaveApply::getId,entityBizLeaveApply.getId())
        );
    }
}
